﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Condimar.Entities;
using Condimar.BussinesLogic;

namespace Condimar.UI
{
    public partial class FrmRegistrarCliente : Form
    {
        [Dependency]
        public IClienteBL clienteBL { get; set; }

        [Dependency]
        public IClienteNaturalBL cliNatBL { get; set; }

        [Dependency]
        public IClienteJuridicoBL cliJuriBL { get; set; }

        bool b;
        public FrmRegistrarCliente()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public Cliente _cliente { get; set; }
        public ClienteJuridico _clienteJuridico { get; set; }
        public ClienteNatural _clienteNatural { get; set; }

        public void Nuevo()
        {
            b = true;
            clienteBindingSource.AddNew();
            clienteNaturalBindingSource.AddNew();
            clienteJuridicoBindingSource.AddNew();
        }

        public void Editar()
        {
            b = false;
            if (_clienteJuridico != null)
            {
                _cliente = _clienteJuridico.Cliente;

                clienteBindingSource.DataSource = _cliente;
                clienteJuridicoBindingSource.DataSource = _clienteJuridico;

                rbtnJuridico.Checked = true;
                rbtnNatural.Enabled = false;
                gbJuridico.Enabled = true;
                gbNatural.Enabled = false;
            }
            if (_clienteNatural!= null)
            {
                _cliente = _clienteNatural.Cliente;

                clienteBindingSource.DataSource = _cliente;
                clienteNaturalBindingSource.DataSource = _clienteNatural;

                rbtnJuridico.Enabled = false;
                rbtnNatural.Checked = true;
                gbJuridico.Enabled = false;
                gbNatural.Enabled = true;
            }

            direccionTextBox.ReadOnly = false;
            telefonoTextBox.ReadOnly = false;
            razonSocialTextBox.ReadOnly = false;
            apPaternoTextBox.ReadOnly = false;
            apMaternoTextBox.ReadOnly = false;
            nombreTextBox.ReadOnly = false;

        }
        private void btnGuardar_Click(object sender, EventArgs e)
        {
            if (rbtnNatural.Checked == true)
            {
                var cliNatural = clienteNaturalBindingSource.Current as ClienteNatural;
                var cliente = clienteBindingSource.Current as Cliente;

                cliNatural.Cliente = cliente;

                var msg = "ERROR";

                if (cliNatural != null)
                {
                    if (b == true)
                    {
                        try
                        {
                            cliNatural.Cliente.Direccion = direccionTextBox.Text.ToUpper();
                            cliNatural.ApMaterno = apMaternoTextBox.Text.ToUpper();
                            cliNatural.ApPaterno = apPaternoTextBox.Text.ToUpper();
                            cliNatural.Nombre = nombreTextBox.Text.ToUpper();
                            cliNatBL.AddClienteNatural(cliNatural);
                            MessageBox.Show("Registro Correcto", "Registro",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                        catch (Exception ex)
                        {
                            msg = ex.Message;
                            MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    if (b == false)
                    {
                        try
                        {
                            cliNatural.Cliente.Direccion = direccionTextBox.Text.ToUpper();
                            cliNatural.ApMaterno = apMaternoTextBox.Text.ToUpper();
                            cliNatural.ApPaterno = apPaternoTextBox.Text.ToUpper();
                            cliNatural.Nombre = nombreTextBox.Text.ToUpper();
                            cliNatBL.UpdateClienteNatural(cliNatural);
                            MessageBox.Show("Modificación Correcto", "Registro",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                        catch (Exception ex)
                        {
                            msg = ex.Message;
                            MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
            else if (rbtnJuridico.Checked == true)
            {
                var cliJuridico = clienteJuridicoBindingSource.Current as ClienteJuridico;
                var cliente = clienteBindingSource.Current as Cliente;

                cliJuridico.Cliente = cliente;

                var msg = "ERROR";

                if (cliJuridico != null)
                {
                    if (b == true)
                    {
                        try
                        {
                            cliJuridico.Cliente.Direccion = direccionTextBox.Text.ToUpper();
                            cliJuridico.RazonSocial = razonSocialTextBox.Text.ToUpper();
                            cliJuriBL.AddClienteJiridico(cliJuridico);
                            MessageBox.Show("Registro Correcto", "Registro",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                        catch (Exception ex)
                        {
                            msg = ex.Message;
                            MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    if (b == false)
                    {
                        try
                        {
                            cliJuridico.Cliente.Direccion = direccionTextBox.Text.ToUpper();
                            cliJuridico.RazonSocial = razonSocialTextBox.Text.ToUpper();
                            cliJuriBL.UpdateClienteJuridico(cliJuridico);
                            MessageBox.Show("Modificación Correcto", "Registro",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                        catch (Exception ex)
                        {
                            msg = ex.Message;
                            MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
        }

        private void rbtnJuridico_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnJuridico.Checked == true)
            {
                gbNatural.Enabled = false;
                gbJuridico.Enabled = true;
            }
            else if (rbtnNatural.Checked == true)
            {
                gbJuridico.Enabled = false;
                gbNatural.Enabled = true;
            }
        }

        private void rucTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void dniTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void telefonoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void nombreTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void FrmRegistrarCliente_Load(object sender, EventArgs e)
        {

        }

        private void rucTextBox_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void dniTextBox_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void apPaternoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void apMaternoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void nombreTextBox_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void btnVerificarJuridico_Click(object sender, EventArgs e)
        {
            var cliJuri = cliJuriBL.GetAllFromClienteJuridicoByRuc(rucTextBox.Text);

            if (rucTextBox.Text.Length != 0)
            {
                if (rucTextBox.Text.Length == 11)
                {
                    if (cliJuri != null)
                    {
                        clienteJuridicoBindingSource.DataSource = cliJuri;
                        clienteBindingSource.DataSource = cliJuri.Cliente;
                        MessageBox.Show("Cliente no Disponible", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    else
                    {
                        direccionTextBox.ReadOnly = false;
                        telefonoTextBox.ReadOnly = false;
                        razonSocialTextBox.ReadOnly = false;
                    }
                }
                else
                {
                    MessageBox.Show("RUC Incorrecto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("Ingrese RUC", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btbVerificarNatural_Click(object sender, EventArgs e)
        {
            var cliNat = cliNatBL.GetAllFromClienteNaturalByDni(dniTextBox.Text);

            if (dniTextBox.Text.Length != 0)
            {
                if (dniTextBox.Text.Length == 8)
                {
                    if (cliNat != null)
                    {
                        clienteNaturalBindingSource.DataSource = cliNat;
                        clienteBindingSource.DataSource = cliNat.Cliente;
                        MessageBox.Show("Cliente no Disponible", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    else
                    {
                        direccionTextBox.ReadOnly = false;
                        telefonoTextBox.ReadOnly = false;
                        apMaternoTextBox.ReadOnly = false;
                        apPaternoTextBox.ReadOnly = false;
                        nombreTextBox.ReadOnly = false;
                    }
                }
                else
                {
                    MessageBox.Show("DNI Incorrecto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("Ingrese DNI", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
